package com.hamster.crm.web;

import com.hamster.crm.mapper.CareerProfitConfigMapper;
import com.hamster.crm.mapper.ManagementProfitConfigMapper;
import com.hamster.crm.mapper.PartnerProfitConfigMapper;
import com.hamster.crm.pojo.*;
import com.hamster.crm.service.BranchProfitConfigService;
import com.hamster.crm.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;
import java.util.Date;
import java.util.List;

/**
 * 分公司返利设置的 Controller
 * @author Tarry
 * @create 2019/12/20 11:08
 */
@RestController
@RequestMapping("profitConfig")
public class ProfitConfigController {
    @Autowired
    private BranchProfitConfigService branchProfitConfigService;
    @Autowired
    private PartnerProfitConfigMapper partnerProfitConfigMapper;
    @Autowired
    private ManagementProfitConfigMapper managementProfitConfigMapper;
    @Autowired
    private CareerProfitConfigMapper careerProfitConfigMapper;
    @Autowired
    private ProjectService projectService;

    /**
     * 获取分公司返利 数据信息
     * @return BranchProfitConfig  状态码200
     */
    @GetMapping("getBranch")
    public ResponseEntity<BranchProfitConfig> getBranchConfig(){
        BranchProfitConfig branchProfitConfig = branchProfitConfigService.get();
        return ResponseEntity.ok(branchProfitConfig);
    }

    /**
     * 设置 分公司返利明细
     * @param branchProfitConfig
     * @return 状态码 200
     */
    @PostMapping("insertBranch")
    public ResponseEntity<Void> insertBranchConfig(@RequestBody BranchProfitConfig branchProfitConfig){
        branchProfitConfigService.insert(branchProfitConfig);
        return ResponseEntity.ok().build();
    }
    @GetMapping("getPartner")
    public ResponseEntity<PartnerProfitConfig> getPartner(){
        PartnerProfitConfig partnerProfitConfig = partnerProfitConfigMapper.selectById("1");
        return ResponseEntity.ok(partnerProfitConfig);
    }
    @GetMapping("getManagement")
    public ResponseEntity<ManagementProfitConfig> getManagementConfig(){
        ManagementProfitConfig managementProfitConfig = managementProfitConfigMapper.selectById("1");
        return ResponseEntity.ok(managementProfitConfig);
    }
    @PostMapping("insertPartner")
    public ResponseEntity<Void> insertPartner(@RequestBody PartnerProfitConfig partnerProfitConfig){
        //如果明细记录存在，则修改，不存在则新增
        partnerProfitConfig.setId("1");
        if (partnerProfitConfigMapper.selectById("1") == null){
            partnerProfitConfigMapper.insert(partnerProfitConfig);
        }else {
            partnerProfitConfigMapper.updateById(partnerProfitConfig);
        }
        return ResponseEntity.ok().build();
    }

    @PostMapping("insertManagement")
    public ResponseEntity<Void> insertManagementConfig(@RequestBody ManagementProfitConfig managementProfitConfig){
        //如果明细记录存在，则修改，不存在则新增
        managementProfitConfig.setId("1");
        if (managementProfitConfigMapper.selectById("1") == null){
            managementProfitConfigMapper.insert(managementProfitConfig);
        }else {
            managementProfitConfigMapper.updateById(managementProfitConfig);
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 给事业部为分公司录入  设置提成
     * @param careerProfitConfig
     * @return
     */
    @PostMapping("insertCareerProfitConfig")
    public ResponseEntity<Void> insertCareerProfitConfig(@RequestBody CareerProfitConfig careerProfitConfig){
        //如果明细记录存在，则修改，不存在则新增
        careerProfitConfig.setId("1");
        if (careerProfitConfigMapper.selectById("1") == null){
            careerProfitConfigMapper.insert(careerProfitConfig);
        }else {
            careerProfitConfigMapper.updateById(careerProfitConfig);
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 获取事业部为分公司录入的提成
     * @return
     */
    @GetMapping("getCareerProfitConfig")
    public ResponseEntity<CareerProfitConfig> getCareerProfitConfig(){
        CareerProfitConfig careerProfitConfig = careerProfitConfigMapper.selectById("1");
        return ResponseEntity.ok(careerProfitConfig);
    }


    /**
     * 分公司返利，获取项目下拉框select
     * @return
     */
    @GetMapping("projectList")
    public ResponseEntity<List<Project>> getProjectList(){
        List<Project> list = projectService.getProjectSelect();
        return ResponseEntity.ok(list);
    }

}
